package com.liao.starapp;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Window;
import android.view.WindowManager;

public class SplashActivity extends AppCompatActivity {

    private final int INTO_GUIDE = 0;
    private final int INTO_MAIN = 1;
    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            switch (msg.what) {
                case INTO_GUIDE:
                    startActivity(new Intent(SplashActivity.this, GuideActivity.class));
                    break;
                case INTO_MAIN:
                    startActivity(new Intent(SplashActivity.this, MainActivity.class));
                    break;
            }
            finish();
            return false;
        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        isFirstRun();
    }

    private void isFirstRun() {
        SharedPreferences sp = getSharedPreferences("config", MODE_PRIVATE);
        boolean isFirst = sp.getBoolean("isFirst", true);
        if (isFirst) {
            sp.edit().putBoolean("isFirst", false).apply();
            handler.sendEmptyMessageDelayed(INTO_GUIDE, 1500);
        } else {
            handler.sendEmptyMessageDelayed(INTO_MAIN, 1500);
        }
    }
}
